<?php
/**
 * SqlError - SQL Exception handler
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 * 
 * @copyright		Copyright 2007-2008
 * @link			http://netbranch.ru
 * @since			Netbranch(tm) v 0.1RC
 * @version			$Revision: 8 $
 * @modifiedby		$LastChangedBy: demerest $
 * @lastmodified	$Date: 2008-03-05 22:34:27 +0000 (Wed, 05 Mar 2008) $
 * @license			http://www.opensource.org/licenses/mit-license.php The MIT License
 */

class SqlError extends Exception {
	/**
	* Error line
	*
	* @var string
	*/
	public  $line;
	
	/**
	* Error file
	*
	* @var string
	*/
	public  $file;
	
	/**
	* Error message
	*
	* @var string
	*/
	public  $message;
	
	/**
	* Error code
	*
	* @var int
	*/
	public  $code;
	
	/**
	* Constructs class
	* There we trace the code with errors
	*
	* @param string $message Error message
	* @return void
	*/
	public function __construct($message, $code = 0) {
		//parent::__construct($message, $code);
		$trace = $this->getTrace();
		
		//print_r($trace);
		
		$this->code    = isset($trace[0]['args'][0]) ? $trace[0]['args'][0] : $this->code;
		$this->message = isset($trace[0]['args'][1]) ? str_replace(':', ' -', $trace[0]['args'][1]) : $message;
		$this->file    = isset($trace[0]['args'][2]) ? $trace[0]['args'][2] : $this->file;
		$this->line    = isset($trace[0]['args'][3]) ? $trace[0]['args'][3] : $this->line;
	}
	
	/**
	* Get error type
	* 
	* @return string error type
	*/
	public function getType() {
		$types = array(
		E_WARNING         => 'E_WARNING',
		E_NOTICE          => 'E_NOTICE',
		E_USER_ERROR      => 'E_USER_ERROR',
		E_USER_WARNING    => 'E_USER_WARNING',
		E_USER_NOTICE     => 'E_USER_NOTICE',
		E_STRICT          => 'E_STRICT'
		);
		
		return !is_object($this->code) && isset($types[$this->code]) ? $types[$this->code] : null;
	}
	
	/**
	* Show error message
	* 
	* @TODO use template here for the error message formatting
	* @return void
	*/
	public function __toString() {
		$file = dirname(__FILE__).'/templates/SqlError.tpl';
		if (file_exists($file) && is_readable($file)) {
			$template = file_get_contents($file);
			$template = str_replace('ERROR_MESSAGE', $this->getMessage(), $template);
			$template = str_replace('ERROR_FILE', $this->getFile(), $template);
			$template = str_replace('ERROR_LINE', $this->getLine(), $template);
			$template = str_replace('ERROR_CODE', $this->getCode(), $template);
			$template = str_replace('ERROR_TYPE', $this->getType(), $template);
			return $template;
		}
		
		return "<div style=\"padding: 5px; font-family: monospace; border: 1px solid red; background: #eee;\">"
		."<strong>Error:</strong> ".$this->getMessage()."<br />"
		."<strong>in file:</strong> ".$this->getFile()."<br />"
		."<strong>on line:</strong> ".$this->getLine()."<br />"
		."<strong>code:</strong> ".$this->getCode()."<br />"
		."<strong>type:</strong> ".$this->getType()."<br />"
		."</div>";
	}
}
?>